<template>
  <div>
    <div class="toolbar">
      <el-input v-model="name" placeholder="按学期名称搜索" style="width: 260px;" clearable />
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="success" @click="openCreate">新增学期</el-button>
    </div>

    <el-table :data="items" stripe style="width: 100%" v-loading="loading">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="startDate" label="开始日期" width="140" />
      <el-table-column prop="endDate" label="结束日期" width="140" />
      <el-table-column prop="active" label="激活" width="100">
        <template #default="{ row }">
          <el-tag :type="row.active ? 'success' : 'info'">{{ row.active ? '是' : '否' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button size="small" @click="openEdit(row)">编辑</el-button>
          <el-popconfirm title="确认删除该学期？" @confirm="doDelete(row.id)">
            <template #reference>
              <el-button size="small" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="editing ? '编辑学期' : '新增学期'" width="560">
      <el-form :model="form" label-width="100px">
        <el-form-item label="名称"><el-input v-model="form.name" /></el-form-item>
        <el-form-item label="开始日期"><el-date-picker v-model="form.startDate" type="date" value-format="YYYY-MM-DD" /></el-form-item>
        <el-form-item label="结束日期"><el-date-picker v-model="form.endDate" type="date" value-format="YYYY-MM-DD" /></el-form-item>
        <el-form-item label="激活">
          <el-switch v-model="activeSwitch" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { listTerms, createTerm, updateTerm, deleteTerm, type Term } from '@/api/grades/terms'

const loading = ref(false)
const items = ref<Term[]>([])
const name = ref('')

const dialogVisible = ref(false)
const editing = ref(false)
const form = ref<Term>({})
const activeSwitch = ref<boolean>(false)

watch(activeSwitch, (val) => {
  form.value.active = val ? 1 : 0
})

function syncActiveSwitch() {
  activeSwitch.value = !!form.value.active
}

async function load() {
  loading.value = true
  try {
    items.value = await listTerms({ name: name.value || undefined })
  } finally {
    loading.value = false
  }
}

function openCreate() {
  editing.value = false
  form.value = { active: 1 }
  syncActiveSwitch()
  dialogVisible.value = true
}

function openEdit(row: Term) {
  editing.value = true
  form.value = { ...row }
  syncActiveSwitch()
  dialogVisible.value = true
}

async function submit() {
  try {
    if (editing.value && form.value.id) {
      await updateTerm(form.value.id, form.value)
      ElMessage.success('更新成功')
    } else {
      await createTerm(form.value)
      ElMessage.success('创建成功')
    }
    dialogVisible.value = false
    await load()
  } catch (e: any) {
    ElMessage.error(e?.message || '操作失败')
  }
}

async function doDelete(id?: number) {
  if (!id) return
  try {
    await deleteTerm(id)
    ElMessage.success('已删除')
    await load()
  } catch (e: any) {
    ElMessage.error(e?.message || '删除失败')
  }
}

onMounted(() => {
  load()
})
</script>

<style scoped>
.toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  align-items: center;
}
</style>